﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using ServerApi.Models;
using ServerApi.Controllers;
using TuyennvLibrary.Utl;
namespace ServerApi.administrator
{
    public partial class AccountManager : System.Web.UI.Page
    {
        AccountModel accModel = new AccountModel();
        public int stt = 1;
        public string title = "Thêm tài khoản mới";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            AdminController.checkLogin(Session, Request, Response);
            AdminController.checkPermission(Page, this.GetType(), Session, Request, Response);
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    int id = int.Parse(Request["id"]);
                    loadinfo(id);
                    title = "Cập nhật ứng dụng";
                }
                Search("");
            }
        }
        private void loadinfo(int id)
        {
            Account acc = accModel.GetByID(id);
            txtUsername.Text = acc.Username;
            txtSyntaxSMS.Text = acc.SyntaxSms.ToString();
            txtName.Text = acc.Name;
            txtServiceId.Text = acc.Serviceid;
            chkStatus.Checked = (acc.Status == 1) ? true : false;
        }
        public void Search(string username)
        {
            DataTable dt = accModel.Search(username);
            GrvListAcc.DataSource = dt;
            GrvListAcc.DataBind();
        }
        protected void LbtnAccept_Click(object sender, EventArgs e)
        {
            Account acc = new Account();
            acc.Username = txtUsername.Text;
            acc.Password = Utilities.md5(txtPassword.Text);
            acc.Oldpassword = txtPassword.Text;
            acc.Name = txtName.Text;
            acc.SyntaxSms = txtSyntaxSMS.Text;
            acc.Serviceid = txtServiceId.Text;
            acc.Status = (chkStatus.Checked) ? 1 : 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                acc.Id = int.Parse(Request["id"]);
                if (accModel.Update(acc))
                {
                    Utilities.Alert(Page, this.GetType(), "Đã cập nhật tài khoản thành công");
                    Response.Redirect("AccountManager.aspx");
                }
            }
            else
            {
                if (accModel.Insert(acc))
                {
                    Utilities.Alert(Page, this.GetType(), "Đã thêm mới tài khoản thành công");
                }
            }
            Search("");
        }

        protected void LbtnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void LbtnSearch_Click(object sender, EventArgs e)
        {
            Search(txtUsername.Text);
        }

        protected void GrvListAcc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("lbtnDel");
                l.Attributes.Add("onclick", "javascript:return " +
                "confirm('Bạn có muốn xóa tài khoản này không? " + "')");
            }
        }

        protected void GrvListAcc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GrvListAcc.DataKeys[e.RowIndex].Value.ToString());
            if (accModel.Delete(id))
            {
                Search("");
                Utilities.Alert(Page, this.GetType(), "Đã xóa tài khoản thành công");
            }
        }
    }
}